<?php

namespace app\admin\controller;

use think\Db;

class Role extends Base
{
    //
    public function index(){
    	// var_dump($_GET['role_name']);die;
    	//   
    	$size=1;
        $title=input('param.')['role_name']??'';
    	$data=Db::table('sys_role')->where([['role_name', 'like', '%'.$title.'%']])->order('sort', 'desc')->paginate($size,false,[
    			'query'=>[
    				'role_name'=>$title
    			]
    		]);
    	// echo Db::getlastsql();
    	// echo 1;
    	$page=input('param.')['page']??1;
    	$this->assign('data',$data);
    	$this->assign('p',$page);
    	$this->assign('size',$size);
    	return $this->fetch();
    }
     // 角色添加
    public function add(){
    	$data=$this->request->param();
    	// var_dump($data);die;
    	$res=Db::table('sys_role')->insert($data);
    	if($res){
    		return json(['msg'=>'ok','code'=>200]);
    	}else{
    		return json_encode(['msg'=>'fail','code'=>500]);
    	}

    }
    // 角色编辑显示
    public function edit(int $id){
    	
    }
    // 角色编辑方法
    public function update(int $id){
    	$data=$this->request->param();
    	// var_dump($data);
    	$res=Db::table('sys_role')->where(['id'=>$id])->update($data);
    	if($res){
    	    return json(['msg'=>'ok','code'=>200]);
    	}else{
    	    return json(['msg'=>'fail']);
    	}
    }
    // 角色删除方法
    public function delete(int $id){
    	$res=Db::table('sys_role')->delete($id);
    	if($res){
    	    return json(['msg'=>'ok','code'=>200]);
    	}else{
    	    return json(['msg'=>'fail']);
    	}
    }
    // 角色权限分配
    public function set(int $role_id){
    	$data=$this->request->param('ids');
    	// var_dump($data);die;
    	if(!$data) return json(['msg'=>'必须有权限','code'=>'200']);
    	if($data && $role_id){
	    	$data=explode(',',$data);
	    	$addData=[];
	    	foreach ($data as $key => $value) {
	    		# code...
	    		$addData[$key]['role_id']=$role_id;
	    		$addData[$key]['menu_id']=$value;
	    	}
	    	$res=Db::table('sys_menu_role')->where('role_id',$role_id)->find();
	    	// var_dump($res);die;
	    	if($res){
	    		// 先删除权限，再增加
	    		$res=Db::table('sys_menu_role')->where('role_id',$role_id)->delete();
	    		// 打断点
	    		// echo Db::getlastsql();
	    		// die;
	    		$res=Db::table('sys_menu_role')->insertAll($addData);
	    		if($res){
	    			return json(['msg'=>'ok','code'=>200]);
	    		}else{
	    			return json(['msg'=>'fail','code'=>500]);
	    		}
	    	}else{
	    		$res=Db::table('sys_menu_role')->insertAll($addData);
	    		if($res){
	    			return json(['msg'=>'ok','code'=>200]);
	    		}else{
	    			return json(['msg'=>'fail','code'=>500]);
	    		}
	    	}
    	}else{
    		return json(['msg'=>'fail','code'=>500]);
    	}    
    }
    // 获取当前角色所有权限
    public function rights(int $role_id){
        $arr=Db::table('sys_menu_role')->field('menu_id')->where(['status_id'=>1,'role_id'=>$role_id])->order('sort','desc')->select();
        $arr = array_column($arr, 'menu_id');
        $arr=Db::table('sys_menu')->where(['id'=>$arr])->order('sort','desc')->select();
        // var_dump($arr);die;
        foreach ($arr as $key => $value) {
            # code...
            $arr[$key]['name']=$value['menu_name'];
            $arr[$key]['pId']=$value['parent_id'];
            // echo $value['pId'];die;
            unset($arr[$key]['menu_name']);
            unset($arr[$key]['parent_id']);
        }
        // var_dump($arr);die;
        $data=Db::table('sys_menu')->where('status_id',1)->order('sort','desc')->select();
        foreach ($data as $key => $value) {
            # code...
            $data[$key]['name']=$value['menu_name'];
            $data[$key]['pId']=$value['parent_id'];
            // echo $value['pId'];die;
            unset($data[$key]['menu_name']);
            unset($data[$key]['parent_id']);
        }
        // $this->assign('data', json_encode($data));
        return json(['code'=>200,'data'=>['select'=>$arr,'all'=>$data]]);
    }
}
